package com.javahash.spring.config;

import com.javahash.spring.service.MongoExchangeRates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

import javax.annotation.PostConstruct;
import javax.inject.Singleton;


//Marks this class as configuration
@Configuration
//Specifies which package to scan
@ComponentScan("com.javahash.spring")
//Enables Spring's annotations 
@EnableWebMvc
//Enables Spring's timed events
@EnableScheduling

public class ApplicationConfiguration {
    @Autowired
    MongoExchangeRates mongoExchangeRates;



    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }


    @Singleton
    @PostConstruct
    public void onStartup() {

        mongoExchangeRates.getLatestRates();

    }

    @Bean
    public ApplicationContext AppContext() {

        return new AnnotationConfigApplicationContext(com.javahash.spring.config.MongoConfiguration.class);
    }


}
